<template>
	<view class="address-edit">
		<view class="form bg-white">
			<u-field v-model="addressObj.contact" label="收货人" placeholder="请填写收货人姓名">
			</u-field>
			<u-field v-model="addressObj.telephone" label="联系方式" placeholder="请填写手机号码">
			</u-field>
			<view @click="showRegion = true">
				<u-field v-model="region" :disabled="true" label="所在地区" placeholder="请选择省、市、区" right-icon="arrow-right">
				</u-field>
			</view>
			<view>
				<u-field v-model="addressObj.address" type="textarea" label="详细地址" placeholder="请填写小区、街道、门牌号等信息"
					:field-style="{flex: 1, height: '200rpx'}" />
			</view>
		</view>
		<view class="m-t-10 m-b-10 bg-white p-20">
			<u-checkbox @click="changeDefault" v-model="addressObj.is_default" shape="circle">
				<text class="xs">设置为默认</text>
			</u-checkbox>
		</view>
		<button class="my-btn bg-primary white br60" @tap="formSubmit">完成</button>
		<u-select v-model="showRegion" :default-value="[defaultValue.province, defaultValue.city, defaultValue.district]"
			mode="mutil-column-auto" @confirm="regionChange" :list="lists"></u-select>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | likeshop开源商城系统
	// +----------------------------------------------------------------------
	// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
	// | gitee下载：https://gitee.com/likeshop_gitee
	// | github下载：https://github.com/likeshop-github
	// | 访问官网：https://www.likeshop.cn
	// | 访问社区：https://home.likeshop.cn
	// | 访问手册：http://doc.likeshop.cn
	// | 微信公众号：likeshop技术社区
	// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
	// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
	// | 禁止对系统程序代码以任何目的，任何形式的再发布
	// | likeshop团队版权所有并拥有最终解释权
	// +----------------------------------------------------------------------
	// | author: likeshop.cn.team
	// +----------------------------------------------------------------------
	import {
		editAddress,
		getOneAddress,
		hasRegionCode,
		addAddress,
		getHandleRegion
	} from '@/api/user';
	import area from '@/utils/area'
	export default {
		data() {
			return {
				addressObj: {
					contact: '',
					telephone: '',
					province: '',
					city: '',
					district: '',
					address: '',
					is_default: false
				},
				region: '',
				addressId: '',
				defaultRegion: ['广东省', '广州市', '番禺区'],
				defaultRegionCode: '440113',
				showRegion: false,
				lists: [],
				defaultValue: {
					province: 0,
					city: 0,
					district: 0
				}
			};
		},
		onLoad: function(options) {
			const app = this;
			this.addressId = parseInt(options.id)
			if (options.id) {
				uni.setNavigationBarTitle({
					title: '编辑地址'
				});
				this.getOneAddressFun();
			} else {
				uni.setNavigationBarTitle({
					title: '添加地址'
				});
				this.getWxAddressFun();
			}
			this.$nextTick(() => {
				this.lists = area
			})

			// 获取当前位置经纬度
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					app.getAddress(res.longitude, res.latitude)
				},
				fail: function(err) {
					uni.showToast({
						title: `${err}`
					})
				}
			});
		},

		// mounted() {
		// 	const script = document.createElement('script');
		// 	script.src =
		// 		"https://map.qq.com/api/gljs?v=1.exp&libraries=service&key=TMABZ-UIXK3-CEX3X-RWZMT-F7EW6-FEF72";
		// 	document.body.appendChild(script);
		// },
		onUnload: function() {
			uni.removeStorageSync('wxAddress');
		},

		methods: {
			// 处理地址信息
			getAddress(longitude, latitude) {
				var geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类
				var location = new TMap.LatLng(Number(latitude), Number(longitude));
				geocoder
					.getAddress({
						location: location
					}) // 将给定的坐标位置转换为地址
					.then((result) => {
						const address = result.result.address_component;
						const obj = {
							province: address.province.indexOf('市') !== -1 ? address.province.split('市')[0] : address.province,
							city: address.city,
							district: address.district,
						}
						this.region = obj.province + " " + obj.city + " " + obj.district
						// 根据坐标转换的城市获取到对应的ID
						getHandleRegion(obj).then(res => {
							this.addressObj.province_id = res.data.province
							this.addressObj.city_id = res.data.city
							this.addressObj.district_id = res.data.district
							this.lists.forEach((provinceItem, provinceIndex) => {
								// 印射对应的省份选项对应下标
								if (provinceItem.value === res.data.province) {
									this.defaultValue.province = provinceIndex;
									provinceItem.children.forEach((cityItem, cityIndex) => {
										// 印射对应的城市选项对应下标
										if (cityItem.value === res.data.city) {
											this.defaultValue.city = cityIndex;
											cityItem.children.forEach((districtItem, districtIndex) => {
												// 印射对应的区域选项对应下标
												if (districtItem.value === res.data.district) {
													this.defaultValue.district = districtIndex;
												}
											})
										}
									})
								}
							})
						})
					});
			},
			async formSubmit() {
				let {
					addressObj: {
						contact,
						telephone,
						province_id,
						city_id,
						district_id,
						is_default,
						address
					},
					addressId,
					region,
				} = this;
				if (!contact) return this.$toast({
					title: '请填写收货人姓名'
				});
				if (!telephone) return this.$toast({
					title: '请填写手机号码'
				});
				if (!region) return this.$toast({
					title: '请选择省、市、区'
				});
				if (!address) return this.$toast({
					title: '请填写小区、街道、门牌号等信息'
				});
				const params = {
					contact,
					telephone,
					province_id: parseInt(province_id),
					city_id: parseInt(city_id),
					district_id: parseInt(district_id),
					is_default: is_default ? 1 : 0,
					id: addressId,
					address
				}
				const {
					code,
					msg
				} = addressId ? await editAddress(params) : await addAddress(params)
				if (code == 1) {
					this.$toast({
						title: msg
					}, {
						tab: 3,
						url: 1
					});
				}
			},
			regionChange(region) {
				this.addressObj.province_id = region[0].value;
				this.addressObj.city_id = region[1].value;
				this.addressObj.district_id = region[2].value;
				this.region = region[0].label + " " + region[1].label + " " + region[2].label
				console.log(this.addressObj, 'this.region');
			},

			getOneAddressFun() {
				getOneAddress(this.addressId).then(res => {
					if (res.code == 1) {
						let {
							city,
							province,
							district
						} = res.data;
						this.addressObj = res.data;
						this.region = `${province} ${city} ${district}`
					}
				});
			},

			getWxAddressFun() {
				let wxAddress = uni.getStorageSync('wxAddress');
				if (!wxAddress) return;
				wxAddress = JSON.parse(wxAddress)
				let {
					userName: contact,
					telNumber: telephone,
					provinceName: province,
					cityName: city,
					detailInfo: address
				} = wxAddress;
				let district = wxAddress.countryName || wxAddress.countyName
				hasRegionCode({
					province,
					city,
					district
				}).then(res => {
					if (res.code == 1) {
						if (res.data.province) {
							this.region = `${province} ${city} ${district}`;
							this.addressObj.contact = contact;
							this.addressObj.telephone = telephone
							this.addressObj.address = address
							this.addressObj.province_id = res.data.province;
							this.addressObj.city_id = res.data.city;
							this.addressObj.district_id = res.data.district;
						}
					}
				});
			}

		}
	};
</script>
<style lang="scss">
	.address-edit {
		padding-top: 10rpx;


		.my-btn {
			margin: 30rpx 26rpx;
			text-align: center;
		}
	}
</style>